// components/index/index.js
const app = getApp();
let timer;
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    //搜索
    searchKey: '',
    //swiper
    sildes: '',
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    //筛选
    regions: '',
    orderBy: [{
      name: '综合',
      id: 0
    }, {
      name: '高薪',
      id: 1
    }, {
      name: '高酬',
      id: 2
    }],
    regionsShow: false,
    orderByShow: false,
    activeLocation: {},
    activeOrderBy: {},
    //列表
    pageSize: 10,
    pageIndex: 1,
    listData: []
  },

  ready() {
    app.$request.jobLayer()
    .then(res => {
      this.setData({
        regions: [{ name: '全重庆', id: 0 }, ...res.data.regions],
        sildes: res.data.sildes,
        activeLocation: { name: '全重庆', id: 0 },
        activeOrderBy: this.data.orderBy[0]
      });
    })
    .then(_ => {
      this.list()
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    searchonChange(e) {
      this.setData({
        searchKey: e.detail
      });
    },
    searchonClear(e) {
      setTimeout(_ => {
        this.list();
      }, 300)
    },
    onClose() {
      this.setData({
        regionsShow: false,
        orderByShow: false,
      });
    },
    regionsShowPopup() {
      this.setData({ regionsShow: true });
    },
    orderByShowPopup() {
      this.setData({ orderByShow: true });
    },
    onCancel() {
      this.setData({
        regionsShow: false,
        orderByShow: false
      });
    },
    regionsOnConfirm(data) {
      this.setData({
        regionsShow: false,
        activeLocation: data.detail.value
      });
      this.list();
    },
    orderByOnConfirm(data) {
      this.setData({
        orderByShow: false,
        activeOrderBy: data.detail.value
      });
      this.list();
    },
    list() {
      let data = {
        searchKey: this.data.searchKey,
        region: this.data.activeLocation.id,
        orderBy: this.data.activeOrderBy.id,
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize
      }
      app.$request.jobSearch(data)
        .then(res => {
          if(res.data.data.length > 0) {
            this.setData({
              listData: [...this.data.listData,...res.data.data]
            })
          }
        })
    },

    goDetail(event) {
      let item = event.currentTarget.dataset.item;
      wx.navigateTo({
        url: '../recruitInfo/recruitInfo',
        events: {
          acceptDataFromOpenedPage: data => {
            console.log(data)
          }
        },
        success: res => {
          res.eventChannel.emit('acceptDataFromOpenerPage', { eid: item.id })
        }
      })
    },
    lower(e) {
      console.log(e)
    },
    scroll(e) {
      timer && clearTimeout(timer);
      timer = setTimeout(_=>{
        let deltaY = e.detail.deltaY < 0;
        if(deltaY) {
          this.setData({
            pageIndex: this.data.pageIndex + 1
          })
          this.list();
        }
      },300);
    }
  }
})
